﻿using Binding的数据校验.cls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Binding的数据校验
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Binding binding=new Binding("Value"){Source=this.slider1};
            binding.UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged;
            RangeValidationRule rvr=new RangeValidationRule ();
            rvr.ValidatesOnTargetUpdated = true;
            binding.ValidationRules.Add(rvr);
            binding.NotifyOnValidationError = true;
            BindingOperations.SetBinding(txtValue, TextBox.TextProperty, binding);
           // txtValue.SetBinding(TextBox.TextProperty,binding);//这两句话都可以

            txtValue.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
        }

        private void ValidationError(object sender, RoutedEventArgs e)
        {
            if (Validation.GetErrors(txtValue).Count > 0)
            {
                txtValue.ToolTip = Validation.GetErrors(txtValue)[0].ErrorContent.ToString();
            }
            else
            {
                txtValue.ToolTip = "";
            }
        }
    }
}
